

* Prepare data for gains from marriage 

use "$data\M_final_dataset.dta" , clear
rename y_survey year
gen hnative= hbc==0
tab hnative
gen wnative= wbc==0
tab wnative 

gen HOM = hbc==wbc
lab var HOM "Homogamous"
gen HET = HOM!=1
lab var HET "Heterogamous"
gen HET_IT =  (hbc==0 & wbc>0) | ( hbc>0 & wbc==0) 
lab var HET_IT "Heterogamous-Italian"
gen HETW = HET==1 & wbc!=0
gen HETH = HET==1 & hbc!=0
gen famt= .
replace famt=1 if wnative==1 & hnative==1
replace famt=2 if wnative==1 & hnative==0
replace famt=3 if wnative==0 & hnative==1
replace famt=4 if wnative==0 & hnative==0
label define famtl 1 "Wife Native-Husband Native" 2 "Wife Native-Husband Foreign" 3 "Wife Foreign-Husband Native" 4 "Wife Foreign-Husband Foreign"
label values famt famtl

* Generate homogamous marriages of natives
preserve
keep if hnative==1&wnative==1
gen nm=1
collapse (sum) nm , by( year)
gen eui=0
save "$data\homogamous_italian.dta" , replace
restore

* Generate number of marriages
gen nm=1
collapse (sum) nm , by( region provres year wbc hbc HOM HET_IT HET famt)

* Merge with single data

merge m:1 provres hbc year using "$data\Msinglemag_country_alltime.dta" , keep(match master) keepusing(Msingle)
tab _merge
drop _merge 
replace Msingle=round(Msingle,1)

merge m:1 provres wbc year using "$data\Fsinglemag_country_alltime.dta" , keep(match master) keepusing(Fsingle)
tab _merge
drop _merge 
replace Fsingle=round(Fsingle,1)

* Merge single other gender 

preserve
use "$data\Msinglemag_country_alltime.dta" , clear
rename hbc eg
rename Msingle Msingle_for_wbc
lab var Msingle_for_wbc "Single males to merge with female nationality"
save "$data\Msinglemag_country_alltime_merge2.dta" , replace
use "$data\Fsinglemag_country_alltime.dta" , clear
rename wbc eg
rename Fsingle Fsingle_for_hbc
lab var Fsingle_for_hbc "Single females to merge with male nationality"
save "$data\Fsinglemag_country_alltime_merge2.dta" , replace
restore
gen eg=wbc
merge m:1 provres eg year using "$data\Msinglemag_country_alltime_merge2.dta" , keep(match master) keepusing(Msingle_for_wbc)
tab _merge
drop _merge 
replace Msingle_for_wbc=round(Msingle_for_wbc,1)
drop eg 
gen eg=hbc
merge m:1 provres eg year using "$data\Fsinglemag_country_alltime_merge2.dta" , keep(match master) keepusing(Fsingle_for_hbc)
tab _merge
drop _merge 
replace Fsingle_for_hbc=round(Fsingle_for_hbc,1)

*Merge with Q vector 

merge m:1 provres hbc year using "$data\Q_merge_male.dta" , keep(match master) 
tab _merge
drop _merge
 
merge m:1 provres wbc year using "$data\Q_merge_female.dta" , keep(match master) 
tab _merge
drop _merge

merge m:1 provres using "$data\population_byprovince.dta" , keep(match master) 
tab _merge
drop _merge

rename qtny_h Qh              
rename qmny_h Qhmale         
rename qtny_w Qw          
rename qfny_w Qwfemale 

* Generate gains to marriage CS2006

preserve
collapse (sum) nm  (mean) Fsingle Msingle Msingle_for_wbc Fsingle_for_hbc Qh Qhmale Qw Qwfemale , by( region provres year wbc hbc HOM  HET HET_IT famt population)
gen phixy = ln((nm^2)/(Fsingle*Msingle))
tabstat phixy , by(famt) stat( mean sd N)

lab var phixy "gains to marriage"
lab var Qh "share of residents of origin hbc of the husband"
lab var Qhmale "share of male residents of origin hbc of the husband"
lab var Qw "share of residents of origin wbc of the wife"
lab var Qwfemale "share of female residents of origin wbc of the wife"
lab var population "total resident population in the year/province"

save "$data\Gain_marriage.dta" , replace
restore



* Include irregular immigrants 

use "$data\M_final_dataset.dta" , clear

rename y_survey year
gen hnative= hbc==0
tab hnative
gen wnative= wbc==0
tab wnative 
gen HOM = hbc==wbc
lab var HOM "Homogamous"
gen HET = HOM!=1
lab var HET "Heterogamous"
gen HET_IT =  (hbc==0 & wbc>0) | ( hbc>0 & wbc==0) 
lab var HET_IT "Heterogamous-Italian"
gen HETW = HET==1 & wbc!=0
gen HETH = HET==1 & hbc!=0

gen famt= .
replace famt=1 if wnative==1 & hnative==1
replace famt=2 if wnative==1 & hnative==0
replace famt=3 if wnative==0 & hnative==1
replace famt=4 if wnative==0 & hnative==0
label define famtl 1 "Wife Native-Husband Native" 2 "Wife Native-Husband Foreign" 3 "Wife Foreign-Husband Native" 4 "Wife Foreign-Husband Foreign"
label values famt famtl

* Generate number of marriages

gen nm=1
collapse (sum) nm , by( region provres year wbc hbc HOM HET_IT HET  famt)

* Merge with single data 

merge m:1 provres hbc year using "$data\Msingle_FINAL.dta" , keep(match master) keepusing(Msingle)
tab _merge
drop _merge 
replace Msingle=round(Msingle,1)

merge m:1 provres wbc year using "$data\Fsingle_FINAL.dta" , keep(match master) keepusing(Fsingle)
tab _merge
drop _merge 
replace Fsingle=round(Fsingle,1)

preserve
use "$data\Msingle_FINAL.dta" , clear
rename hbc eg
rename Msingle Msingle_for_wbc
lab var Msingle_for_wbc "Single males to merge with female nationality"
save "$data\Msingle_FINAL_merge2.dta" , replace
use "$data\Fsingle_FINAL.dta" , clear
rename wbc eg
rename Fsingle Fsingle_for_hbc
lab var Fsingle_for_hbc "Single females to merge with male nationality"
save "$data\Fsingle_FINAL_merge2.dta" , replace
restore
gen eg=wbc
merge m:1 provres eg year using "$data\Msingle_FINAL_merge2.dta" , keep(match master) keepusing(Msingle_for_wbc)
tab _merge
drop _merge 
replace Msingle_for_wbc=round(Msingle_for_wbc,1)
drop eg 
gen eg=hbc
merge m:1 provres eg year using "$data\Fsingle_FINAL_merge2.dta" , keep(match master) keepusing(Fsingle_for_hbc)
tab _merge
drop _merge 
replace Fsingle_for_hbc=round(Fsingle_for_hbc,1)

* Merge with Q vector

merge m:1 provres hbc year using "$data\Q_merge_male.dta" , keep(match master) 
tab _merge
drop _merge
 
merge m:1 provres wbc year using "$data\Q_merge_female.dta" , keep(match master) 
tab _merge
drop _merge

merge m:1 provres using "$data\population_byprovince.dta" , keep(match master) 
tab _merge
drop _merge

rename qtny_h Qh              
rename qmny_h Qhmale         
rename qtny_w Qw          
rename qfny_w Qwfemale     

* Singles for separations

preserve
keep provres year hbc wbc Msingle Fsingle Msingle_for_wbc Fsingle_for_hbc Qh Qhmale Qw Qwfemale
rename year ym
keep if ym>=1998
save "$data\merge_singles_for_separations.dta" , replace
restore

* Generate gains to marriage CS2006

collapse (sum) nm  (mean) Fsingle Msingle Msingle_for_wbc Fsingle_for_hbc Qh Qhmale Qw Qwfemale , by( region provres year wbc hbc HOM  HET HET_IT famt population)
gen phixy = ln((nm^2)/(Fsingle*Msingle))
tabstat phixy , by(famt) stat( mean sd N)

lab var phixy "gains to marriage"
lab var Qh "share of residents of origin hbc of the husband"
lab var Qhmale "share of maleresidents of origin hbc of the husband"
lab var Qw "share of residents of origin wbc of the wife"
lab var Qwfemale "share of female residents of origin wbc of the wife"
lab var population "total resident population in the year/province"

save "$data\Gain_marriage_robust.dta" , replace



******************* NATIVE HUSBAND - FOREIGN WIFE **************************
use "$data\Gain_marriage_robust.dta" , clear
keep if hbc==0

* Treatment
gen nat= wbc
merge m:1 nat using "$data\CodiciStatiEsteri.dta" , keepusing(EUII)
tab _merge
drop _merge
replace EUII=4 if nat==224 
replace EUII=4 if nat==258  
replace EUII=4 if nat==259  
replace EUII=4 if nat==260  
replace EUII=4 if nat==261  
replace EUII=6 if nat==317  
replace EUII=0 if nat==0  
lab define eui3 0 "ITA" 1 "EU15"  2 "EU2004" 3 "EU2007" 4 "EU_Other" 5 "Africa" 6 "Asia" 7 "America_South" 8 "OECD" 
label values EUII eui3

tab EUII, gen(euii)
rename euii1 ita
rename euii2 eu15
rename euii3 eu2004
rename euii4 eu2007
rename euii5 euother
rename euii6 africa
rename euii7 asia
rename euii8 americas
rename euii9 oecd
codebook EUII
drop if EUII==.

* Time
tab year
drop if year==1995 
drop if year==1996 
drop if year==1997
tab year, gen(t)
rename t1 t1998
rename t2 t1999
rename t3 t2000
rename t4 t2001
rename t5 t2002
rename t6 t2003
rename t7 t2004
rename t8 t2005
rename t9 t2006
rename t10 t2007
rename t11 t2008
rename t12 t2009
rename t13 t2010
rename t14 t2011
rename t15 t2012
gen post2003= year>=2003
gen post2004= year>=2004
gen post2006= year>=2006
gen post2007= year>=2007

* Interaction
gen eu2004post2003= eu2004*post2003
gen eu2004post2004= eu2004*post2004
gen eu2004post2006= eu2004*post2006
gen eu2004post2007= eu2004*post2007
gen eu2007post2003= eu2007*post2003
gen eu2007post2004= eu2007*post2004
gen eu2007post2006= eu2007*post2006
gen eu2007post2007= eu2007*post2007

forvalues t=1998/2012 {
gen eu2004t`t' = eu2004*t`t'
gen eu2007t`t'  = eu2007*t`t'
}
drop if phixy==.
xtset provres 
order provres year
sort provres year
xtsum phixy 


* Add singles with no irregulars
rename phixy phixy0
merge 1:1 year hbc wbc provres using "$data\Gain_marriage", keepusing(phixy)
drop if _m==2
rename phixy phixy_noirreg
rename phixy0 phixy

save "$data\gains_marriage_nativehusband.dta" , replace


******************* NATIVE WIFE - FOREIGN HUSBAND **************************
use "$data\Gain_marriage_robust.dta" , clear
keep if wbc==0

* Treatment
gen nat = hbc
merge m:1 nat using "$data\CodiciStatiEsteri.dta" , keepusing(EUII)
tab _merge
drop _merge
replace EUII=4 if nat==224 
replace EUII=4 if nat==258  
replace EUII=4 if nat==259  
replace EUII=4 if nat==260  
replace EUII=4 if nat==261  
replace EUII=6 if nat==317  
replace EUII=0 if nat==0 
lab define eui3 0 "ITA" 1 "EU15"  2 "EU2004" 3 "EU2007" 4 "EU_Other" 5 "Africa" 6 "Asia" 7 "America_South" 8 "OECD" 
label values EUII eui3
tab EUII, gen(euii)
rename euii1 ita
rename euii2 eu15
rename euii3 eu2004
rename euii4 eu2007
rename euii5 euother
rename euii6 africa
rename euii7 asia
rename euii8 americas
rename euii9 oecd
codebook EUII
drop if EUII==.

* Time 
tab year
drop if year==1995 
drop if year==1996 
drop if year==1997
tab year, gen(t)
rename t1 t1998
rename t2 t1999
rename t3 t2000
rename t4 t2001
rename t5 t2002
rename t6 t2003
rename t7 t2004
rename t8 t2005
rename t9 t2006
rename t10 t2007
rename t11 t2008
rename t12 t2009
rename t13 t2010
rename t14 t2011
rename t15 t2012
gen post2003= year>=2003
gen post2004= year>=2004
gen post2006= year>=2006
gen post2007= year>=2007

* Interaction
gen eu2004post2003= eu2004*post2003
gen eu2004post2004= eu2004*post2004
gen eu2004post2006= eu2004*post2006
gen eu2004post2007= eu2004*post2007
gen eu2007post2003= eu2007*post2003
gen eu2007post2004= eu2007*post2004
gen eu2007post2006= eu2007*post2006
gen eu2007post2007= eu2007*post2007

forvalues t=1998/2012 {
gen eu2004t`t' = eu2004*t`t'
gen eu2007t`t'  = eu2007*t`t'
}

drop if phixy==.
xtset provres 
order provres year
sort provres year
xtsum phixy 

* Add singles with no irregulars
rename phixy phixy0
merge 1:1 year hbc wbc provres using "$data\Gain_marriage", keepusing(phixy)
drop if _m==2
rename phixy phixy_noirreg
rename phixy0 phixy

save "$data\gains_marriage_nativewife.dta" , replace




